package com.jinmdz.fmis.core.wrapper;

import com.jinmdz.fmis.core.model.RequestInfo;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * HttpServletRequest封装处理类
 *
 * @author LiCongLu
 * @date 2019-12-26 17:12
 */
public final class RequestWrapper {
    /**
     * 获取请求相关参数
     *
     * @param request 请求
     * @return RequestInfo
     * @author LiCongLu
     * @date 2019-12-27 08:46
     */
    public static RequestInfo getRequestInfo(HttpServletRequest request) {
        RequestInfo requestInfo = new RequestInfo();
        requestInfo.setServletPath(request.getServletPath());
        requestInfo.setContextPath(request.getContextPath());
        requestInfo.setRequestURI(request.getRequestURI());
        requestInfo.setRequestURL(request.getRequestURL().toString());

        requestInfo.setRealPath(request.getServletContext().getRealPath("/"));

        requestInfo.setScheme(request.getScheme());
        requestInfo.setServerName(request.getServerName());
        requestInfo.setServerPort(request.getServerPort());
        return requestInfo;
    }

    /**
     * 获取基地址
     *
     * @param request 请求
     * @return
     * @author GuanKui
     * @date 2020/3/4  16:10
     */
    public static String getBaserUrl(HttpServletRequest request) {
        String baseUrl = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
        return baseUrl;
    }

    /**
     * 获取完整路径
     *
     * @param request 请求
     * @return
     * @author LiCongLu
     * @date 2020-03-06 23:29
     */
    public static String getFullPathForUrl(HttpServletRequest request, String url) {
        String baseUrl = getBaserUrl(request);
        if (!StringUtils.isEmpty(url)) {
            if (!url.startsWith("/")) {
                return baseUrl + "/" + url;
            }
        }
        return baseUrl + url;
    }
}
